{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Flips to Make a OR b Equal to c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minFlips"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #或运算的最小翻转次数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你三个正整数&nbsp;<code>a</code>、<code>b</code> 和 <code>c</code>。</p>\n",
    "\n",
    "<p>你可以对 <code>a</code> 和 <code>b</code>&nbsp;的二进制表示进行位翻转操作，返回能够使按位或运算&nbsp; &nbsp;<code>a</code> OR <code>b</code> == <code>c</code>&nbsp;&nbsp;成立的最小翻转次数。</p>\n",
    "\n",
    "<p>「位翻转操作」是指将一个数的二进制表示任何单个位上的 1 变成 0 或者 0 变成 1 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/11/sample_3_1676.png\" style=\"height: 87px; width: 260px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>a = 2, b = 6, c = 5\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>翻转后 a = 1 , b = 4 , c = 5 使得 <code>a</code> OR <code>b</code> == <code>c</code></pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>a = 4, b = 2, c = 7\n",
    "<strong>输出：</strong>1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>a = 1, b = 2, c = 3\n",
    "<strong>输出：</strong>0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= a &lt;= 10^9</code></li>\n",
    "\t<li><code>1 &lt;= b&nbsp;&lt;= 10^9</code></li>\n",
    "\t<li><code>1 &lt;= c&nbsp;&lt;= 10^9</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-flips-to-make-a-or-b-equal-to-c](https://leetcode.cn/problems/minimum-flips-to-make-a-or-b-equal-to-c/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-flips-to-make-a-or-b-equal-to-c](https://leetcode.cn/problems/minimum-flips-to-make-a-or-b-equal-to-c/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['2\\n6\\n5', '4\\n2\\n7', '1\\n2\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        d = (a | b) ^ c\n",
    "        return d.bit_count() + (a & b & d).bit_count()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        k=0\n",
    "        for i in range(31):\n",
    "            x=(a>>i)&1\n",
    "            y=(b>>i)&1\n",
    "            w =(c>>i)&1\n",
    "            t=x+y\n",
    "            if w:\n",
    "                k +=1 if 0==t else 0 \n",
    "            else:k +=t\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(31):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans=0\n",
    "        for i in range(32):\n",
    "            bit_a,bit_b,bit_c=(a>>i)&1,(b>>i)&1,(c>>i)&1\n",
    "            if bit_c==0:\n",
    "                ans+=bit_a+bit_b\n",
    "            else:\n",
    "                ans+=int(bit_a+bit_b==0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        s = (a|b) ^ c\n",
    "        return sum(map(lambda x:(x&s).bit_count(),[a,b,c]))\n",
    "        # return sum(map(lambda x:x.bit_count(),[(a|b)^c,a&b&(~c)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        k=0\n",
    "        for i in range(31):\n",
    "            x=(a>>i)&1\n",
    "            y=(b>>i)&1\n",
    "            w =(c>>i)&1\n",
    "            if w==0:\n",
    "                k += x+y \n",
    "            else:\n",
    "                k +=1 if x==y==0 else 0\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        s = 0\n",
    "        for i in range(32):\n",
    "            x = a & 1\n",
    "            y = b & 1\n",
    "            z = c & 1\n",
    "            a = a >> 1\n",
    "            b = b >> 1\n",
    "            c = c >> 1\n",
    "            if z == 0:\n",
    "                s = s + x + y\n",
    "            else:\n",
    "                s = s + 1 - (x | y)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        res = 0\n",
    "\n",
    "        while a>0 or b>0 or c>0:\n",
    "            bit_a, bit_b, bit_c = a%2, b%2, c%2\n",
    "            if not bit_c:\n",
    "                res += bit_a+bit_b\n",
    "            else:\n",
    "                if not bit_a and not bit_b:\n",
    "                    res += 1\n",
    "\n",
    "            a, b, c = a//2, b//2, c//2\n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            ab = (a >> i) & 1\n",
    "            bb = (b >> i) & 1\n",
    "            cb = (c >> i) & 1\n",
    "            if cb == 0:\n",
    "                ans += ab + bb\n",
    "            else:\n",
    "                ans += int(ab == 0 and bb == 0)\n",
    "\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bits_a, bits_b, bits_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bits_c == 0:\n",
    "                ans += (bits_a + bits_b)\n",
    "            else:\n",
    "                ans += 1 - int(bits_a | bits_b)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        # 是或运算，不是异或运算\n",
    "        cnt=0\n",
    "        for i in range(32):\n",
    "            a_bit =(a>>i)&1\n",
    "            b_bit=(b>>i)&1\n",
    "            c_bit =(c>>i)&1\n",
    "            if c_bit:\n",
    "                cnt+= a_bit==b_bit==0\n",
    "            else:\n",
    "                cnt+= a_bit+b_bit\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        res = 0\n",
    "\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "\n",
    "            if bit_c == 0:\n",
    "                res += bit_a + bit_b\n",
    "            else:\n",
    "                res += int(bit_a + bit_b == 0)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        aa = [0] * 32\n",
    "        bb = [0] * 32\n",
    "        cc = [0] * 32\n",
    "\n",
    "        def get_bits(arr, v):\n",
    "            idx = 0\n",
    "            while v != 0:\n",
    "                arr[idx] = v & 1\n",
    "                v >>= 1\n",
    "                idx += 1\n",
    "        get_bits(aa,a)\n",
    "        get_bits(bb,b)\n",
    "        get_bits(cc,c)\n",
    "        ans = 0\n",
    "        for i in range(len(aa)):\n",
    "            if aa[i] | bb[i] != cc[i]:\n",
    "                if cc[i] == 0:\n",
    "                    ans += aa[i] + bb[i]\n",
    "                else:\n",
    "                    ans += 1\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        #要使得a or b == c，则需要把a or b 的每一个比特位变成c\n",
    "        # 遍历每一位，如果 c == 1，则保证该位上，a b有一个1即可。如果c==0，则保证该位上 a b都是0\n",
    "        #如何遍历十进制的每一位。32位bit足以。\n",
    "        count = 0\n",
    "        for i in range(32):\n",
    "            a_bit = a & (1 << i)\n",
    "            b_bit = b & (1 << i)\n",
    "            c_bit = c & (1 << i)\n",
    "            if c_bit:\n",
    "                if a_bit or b_bit:\n",
    "                    continue\n",
    "                else:\n",
    "                    count += 1\n",
    "            else:\n",
    "                if a_bit:\n",
    "                    count += 1\n",
    "                if b_bit:\n",
    "                    count += 1\n",
    "        return count\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        if a < b:\n",
    "            a, b = b, a\n",
    "\n",
    "        bin_a, bin_b, bin_c = bin(a)[2:], bin(b)[2:], bin(c)[2:]\n",
    "\n",
    "        bin_b = '0' * (len(bin_a) - len(bin_b)) + bin_b\n",
    "        temp = []\n",
    "        for i in range(len(bin_a)):\n",
    "            temp.append(int(bin_a[i]) + int(bin_b[i]))\n",
    "\n",
    "        if len(bin_a) > len(bin_c):\n",
    "            bin_c = '0' * (len(bin_a) - len(bin_c)) + bin_c\n",
    "        else:\n",
    "            temp = [0] * (len(bin_c) - len(bin_a)) + temp\n",
    "        res = 0\n",
    "        for i in range(len(temp)):\n",
    "            cur = bin_c[i]\n",
    "            if cur == '0':\n",
    "                res += temp[i]\n",
    "            else:\n",
    "                res += 1 if temp[i] == 0 else 0\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, ( b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        a = bin(a)[2:]\n",
    "        b = bin(b)[2:]\n",
    "        c = bin(c)[2:]\n",
    "        lens = max(len(a),len(b),len(c))\n",
    "        a = \"0\" * (lens - len(a)) + a\n",
    "        b = \"0\" * (lens - len(b)) + b\n",
    "        c = \"0\" * (lens - len(c)) + c\n",
    "        res = 0\n",
    "        for i in range(lens):\n",
    "            now = c[i]\n",
    "            if now == \"0\":\n",
    "                if a[i] == \"1\":\n",
    "                    res += 1\n",
    "                if b[i] == \"1\":\n",
    "                    res += 1\n",
    "            else:\n",
    "                if a[i] == \"1\":\n",
    "                    continue\n",
    "                elif b[i] == \"1\":\n",
    "                    continue\n",
    "                else:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        flip_count = 0\n",
    "    \n",
    "        while a > 0 or b > 0 or c > 0:\n",
    "            # 获取 a, b, c 的最低位\n",
    "            a_bit, b_bit, c_bit = a & 1, b & 1, c & 1\n",
    "            \n",
    "            # 计算 a OR b 的最低位\n",
    "            or_bit = a_bit | b_bit\n",
    "\n",
    "            # 检查是否需要进行翻转\n",
    "            if or_bit != c_bit:\n",
    "                if c_bit == 0:\n",
    "                    # a OR b 的结果是 1，但 c 的对应位是 0\n",
    "                    # 所以需要把 a 或 b 的 1 翻转为 0\n",
    "                    flip_count += a_bit + b_bit\n",
    "                else:\n",
    "                    # a OR b 的结果是 0，但 c 的对应位是 1\n",
    "                    # 所以需要把 a 或 b 的 0 翻转为 1\n",
    "                    flip_count += 1\n",
    "            \n",
    "            # 右移以检查下一位\n",
    "            a, b, c = a >> 1, b >> 1, c >> 1\n",
    "        \n",
    "        return flip_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            low_bit_a = a >> i & 1\n",
    "            low_bit_b = b >> i & 1\n",
    "            low_bit_c = c >> i & 1\n",
    "            if low_bit_c == 0:\n",
    "                ans += low_bit_a + low_bit_b\n",
    "            else:\n",
    "                if not low_bit_a and not low_bit_b:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        while a | b | c:\n",
    "            if c & 1:\n",
    "                ans += a & 1 == b & 1 == 0\n",
    "            else:\n",
    "                ans += (a & 1) + (b & 1)\n",
    "            a >>= 1\n",
    "            b >>= 1\n",
    "            c >>= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ln = max(a.bit_length(), b.bit_length(), c.bit_length())\n",
    "        ans = 0\n",
    "        for i in range(ln):\n",
    "            ai,bi,ci = (a>>i)&1,(b>>i)&1,(c>>i)&1\n",
    "            if ci ==0:\n",
    "                ans += ai+bi\n",
    "            else:\n",
    "                if ai+bi ==0:\n",
    "                    ans +=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ret = 0\n",
    "        get_bit = lambda num, idx: (num >> idx) & 1\n",
    "        for i in range(32):\n",
    "            _a, _b, _c = get_bit(a, i), get_bit(b, i), get_bit(c, i)\n",
    "            if _c == 0:\n",
    "                ret += _a + _b\n",
    "            elif _a == 0 and _b == 0:\n",
    "                ret += 1\n",
    "        return ret\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "\n",
    "        ans = 0\n",
    "\n",
    "        for i in range(32):\n",
    "\n",
    "            bit_a, bit_b, bit_c = (a>>i)&1, (b>>i)&1, (c>>i)&1\n",
    "\n",
    "            if bit_c == 0:\n",
    "\n",
    "                ans += int(bit_a) + int(bit_b)\n",
    "\n",
    "            else:\n",
    "\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        k=0\n",
    "        for i in range(31):\n",
    "            x=(a>>i)&1\n",
    "            y=(b>>i)&1\n",
    "            w =(c>>i)&1\n",
    "            t=x+y\n",
    "            if w:\n",
    "                k +=0==t \n",
    "            else:k +=t\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(31):\n",
    "            bit_a = a  & 1\n",
    "            bit_b = b  & 1\n",
    "            bit_c = c  & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "            a >>= 1\n",
    "            b >>= 1\n",
    "            c >>= 1\n",
    "\n",
    "        return ans\n",
    "            \n",
    "                \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        k=0\n",
    "        for i in range(31):\n",
    "            x=(a>>i)&1\n",
    "            y=(b>>i)&1\n",
    "            w =(c>>i)&1\n",
    "            t=x+y\n",
    "            if w:\n",
    "                k +=0==t \n",
    "            else:k +=t\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        res=0\n",
    "        for i in range(31):\n",
    "            request=c>>i & 1\n",
    "            provide = (a>>i & 1)+(b>>i & 1)\n",
    "            if request==1 and provide==0:\n",
    "                res+=1\n",
    "            elif request==0:\n",
    "                res+=provide\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i)&1, (b >> i)&1, (c >> i)&1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(31):\n",
    "            bit_a = a >> i & 1\n",
    "            bit_b = b >> i & 1\n",
    "            bit_c = c >> i & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "            # a >>= 1\n",
    "            # b >>= 1\n",
    "            # c >>= 1\n",
    "\n",
    "        return ans\n",
    "            \n",
    "                \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        k=0\n",
    "        for i in range(31):\n",
    "            x=(a>>i)&1\n",
    "            y=(b>>i)&1\n",
    "            w =(c>>i)&1\n",
    "            if w==0:\n",
    "                k += x+y \n",
    "            else:\n",
    "                k +=1 if x==y==0 else 0\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            ra, rb, rc = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if rc == 0:\n",
    "                res += ra + rb\n",
    "            else:\n",
    "                res += int(ra + rb == 0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int: \n",
    "      ans=0\n",
    "\n",
    "      for  i in range(32):\n",
    "        #判断该数 二进制的该位置上的数是多  (a>>i)&1==a的第i上的二进制数是多少\n",
    "        bit_a,bit_b,bit_c=(a>>i)&1,(b>>i)&1,(c>>i)&1\n",
    "        if bit_c==0:\n",
    "          ans+=bit_a+bit_b# 01 10 都是翻转1次 11反转两次 \n",
    "        else:\n",
    "          ans+=int(bit_a+bit_b==0)\n",
    "      return ans\n",
    "# 设 a、b 和 c 二进制表示的第 i 位分别为 bit_a、bit_b 和 bit_c，根据 bit_c 的值，会有以下两种情况：\n",
    "\n",
    "# 若 bit_c 的值为 0，那么 bit_a 和 bit_b 必须都为 0，需要的翻转次数为 bit_a + bit_b；\n",
    "\n",
    "# 若 bit_c 的值为 1，那么 bit_a 和 bit_b 中至少有一个为 1，只有当它们都为 0 时，才需要 1 次翻转；\n",
    "\n",
    "# 我们将每一位的翻转次数进行累加，在枚举完所有位之后，就得到了最小翻转次数。\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ln = max(a.bit_length(), b.bit_length(), c.bit_length())\n",
    "        ans = 0\n",
    "        for i in range(ln):\n",
    "            ai,bi,ci = (a>>i)&1,(b>>i)&1,(c>>i)&1\n",
    "            # if ci ==0:\n",
    "            #     ans += ai+bi\n",
    "            # else:\n",
    "            #     if ai+bi ==0:\n",
    "            #         ans +=1\n",
    "            if ai | bi != ci:\n",
    "                ans += 1\n",
    "                if ai & bi == 1:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        a = bin(a)[2:]\n",
    "        b = bin(b)[2:]\n",
    "        c = bin(c)[2:]\n",
    "        max_length = max(len(a), len(b), len(c))\n",
    "        a = '0' * (max_length - len(a)) + a\n",
    "        b = \"0\" * (max_length - len(b)) + b\n",
    "        c = '0' * (max_length - len(c)) + c\n",
    "        res = 0\n",
    "        for i in range(max_length):\n",
    "            if c[i] == \"0\":\n",
    "                if b[i] == \"1\":\n",
    "                    res += 1\n",
    "                if a[i] == \"1\":\n",
    "                    res += 1\n",
    "            else:\n",
    "                if b[i] == \"0\" and a[i] == \"0\":\n",
    "                    res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        def f(x):\n",
    "            res = 0\n",
    "            while x != 0:\n",
    "                x &= x-1\n",
    "                res += 1\n",
    "            return res\n",
    "        # s = (a|b) ^ c\n",
    "        # return sum(map(lambda x:f(x&s),[a,b,c]))\n",
    "        return sum(map(f,[(a|b)^c,a&b&(~c)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        res = 0\n",
    "        while a > 0 or b > 0 or c > 0:\n",
    "            if ((a | b) & 1) != (c & 1):\n",
    "                if (c & 1) == 1:\n",
    "                    res += 1\n",
    "                else:\n",
    "                    if ((a & 1) == 1) and ((b & 1) == 1):\n",
    "                        res += 2\n",
    "                    else:\n",
    "                        res += 1\n",
    "            c >>= 1\n",
    "            a >>= 1\n",
    "            b >>= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            ai = (a>>i)&1\n",
    "            bi = (b>>i)&1\n",
    "            ci = (c>>i)&1\n",
    "            if ci ==0:\n",
    "                ans += ai+bi\n",
    "            else:\n",
    "                if ai+bi ==0:\n",
    "                    ans +=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        # s = (a|b) ^ c\n",
    "        # return sum(map(lambda x:f(x&s),[a,b,c]))\n",
    "        return sum(map(lambda x:x.bit_count(),[(a|b)^c,a&b&(~c)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        s = (a|b) ^ c\n",
    "        x1,x2 = a&s, b&s\n",
    "        y = s&c\n",
    "        print(s,x1,x2,y)\n",
    "        return sum(map(lambda x:x.bit_count(),[x1,x2,y]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a>>i) & 1, (b>>i) & 1, (c>>i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        s = (a|b) ^ c\n",
    "        return sum(map(lambda x:(x&s).bit_count(),[a,b,c]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        d = bin((a|b)^c)[2:]\n",
    "        a = bin(a)[2:]\n",
    "        b = bin(b)[2:]\n",
    "        c = bin(c)[2:]\n",
    "        count = d.count(\"1\")\n",
    "        if count>0:\n",
    "            for i in range(1, len(d)+1):\n",
    "                if d[-i]==\"1\":\n",
    "                    if i>len(c) or c[-i]==\"0\":\n",
    "                        if i<=len(a) and i<=len(b) and a[-i]==\"1\" and b[-i]==\"1\":\n",
    "                            count += 1\n",
    "        return count\n",
    "                           \n",
    "\n",
    "        return count\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        s = (a|b) ^ c\n",
    "        x1,x2 = a&s, b&s\n",
    "        y = s&c\n",
    "        # print(s,x1,x2,y)\n",
    "        return sum(map(lambda x:x.bit_count(),[x1,x2,y]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        cnt = 0\n",
    "        for i in range(31):\n",
    "            bit_a = (a >> i) & 1\n",
    "            bit_b = (b >> i) & 1\n",
    "            bit_c = (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                cnt += bit_a+bit_b\n",
    "            else:\n",
    "                cnt += int(bit_a+bit_b == 0)\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c:\n",
    "                ans += 1 - (bit_a or bit_b)\n",
    "            else:\n",
    "                ans += (bit_a + bit_b)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        res = 0\n",
    "        for i in range(32):\n",
    "            bitc = c >> i & 1\n",
    "            bita = a >> i & 1\n",
    "            bitb = b >> i & 1\n",
    "            if bitc == 0:\n",
    "                res += bita + bitb\n",
    "            else:\n",
    "                if bita == 0 and bitb == 0:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        # a = bin(a)[2:]\n",
    "        # b = bin(b)[2:]\n",
    "        # c = bin(c)[2:]\n",
    "        # max_length = max(len(a), len(b), len(c))\n",
    "        # a = '0' * (max_length - len(a)) + a\n",
    "        # b = \"0\" * (max_length - len(b)) + b\n",
    "        # c = '0' * (max_length - len(c)) + c\n",
    "        # res = 0\n",
    "        # for i in range(max_length):\n",
    "        #     if c[i] == \"0\":\n",
    "        #         if b[i] == \"1\":\n",
    "        #             res += 1\n",
    "        #         if a[i] == \"1\":\n",
    "        #             res += 1\n",
    "        #     else:\n",
    "        #         if b[i] == \"0\" and a[i] == \"0\":\n",
    "        #             res += 1\n",
    "        # return res\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a>>i) & 1, (b>>i) & 1, (c>>i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0 \n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            x, y, z = a & (1 << i), b & (1<<i), c & (1<<i)\n",
    "            if x | y != z:\n",
    "                if z == 0: \n",
    "                    if x == y: ans += 2\n",
    "                    else: ans += 1\n",
    "                else: ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        b_a, b_b, b_c = bin(a)[2:], bin(b)[2:], bin(c)[2:]\n",
    "        i, j, k = len(b_a) - 1, len(b_b) - 1, len(b_c) - 1\n",
    "        ans = 0\n",
    "        while i >=0 or j >=0 or k >=0:\n",
    "            s_a = b_a[i] if i >= 0 else '0'\n",
    "            s_b = b_b[j] if j >= 0 else '0'\n",
    "            s_c = b_c[k] if k >= 0 else '0'\n",
    "            #s_c为1\n",
    "            if s_c == '1' and s_a != '1' and s_b != '1':\n",
    "                ans += 1\n",
    "            if s_c == '0':\n",
    "                ans += int(s_a == '1') + int(s_b == '1')\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "            k -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        rst = 0\n",
    "        for i in range(0, 32):\n",
    "            bit_a = a >> i & 1\n",
    "            bit_b = b >> i & 1\n",
    "            bit_c = c >> i & 1\n",
    "            if bit_c == 0:\n",
    "                rst += bit_a + bit_b\n",
    "            else:\n",
    "                rst += 1 if (bit_a + bit_b) == 0 else 0\n",
    "        return rst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        r = 0\n",
    "        while a != 0 or b != 0 or c != 0:\n",
    "            m = c & 0b01\n",
    "            if m == 0:\n",
    "                r += 1 if a & 0b01 == 1 else 0\n",
    "                r += 1 if b & 0b01 == 1 else 0\n",
    "            else:\n",
    "                r += 1 if b & 0b01 == 0 and a & 0b01 == 0 else 0\n",
    "            a = a >> 1\n",
    "            b = b >> 1\n",
    "            c = c >> 1\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        aa, bb, cc = bin(a)[2:], bin(b)[2:], bin(c)[2:]\n",
    "        ll = max([len(aa), len(bb), len(cc)])\n",
    "        aa = '0'*(ll-len(aa))+aa\n",
    "        bb = '0'*(ll-len(bb))+bb\n",
    "        cc = '0'*(ll-len(cc))+cc\n",
    "\n",
    "        flips = 0\n",
    "        for ix in range(len(cc)):\n",
    "            if cc[ix] == '0':\n",
    "                flips += (aa[ix]=='1')+(bb[ix]=='1')\n",
    "            else:\n",
    "                flips += 1 if aa[ix]=='0' and bb[ix]=='0' else 0\n",
    "\n",
    "        return flips"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        res = 0\n",
    "        while a or b or c:\n",
    "            aa, bb, cc = a % 2, b % 2, c % 2\n",
    "            a, b, c = a // 2, b // 2, c // 2\n",
    "            if cc == 1: res += (1 if aa == 0 and bb == 0 else 0)\n",
    "            else: res += (aa + bb)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            l, m, n = a >> i & 1, b >> i & 1, c >> i & 1\n",
    "            if n != 0:\n",
    "                if l == 0 and m == 0:\n",
    "                    ans += 1\n",
    "            else:\n",
    "                ans += l + m\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        n = max(a.bit_length(), b.bit_length(), c.bit_length())\n",
    "        ans = 0\n",
    "        for _ in range(n):\n",
    "            if (a & 1) | (b & 1) == c & 1:\n",
    "                pass\n",
    "            elif c & 1 == 1:\n",
    "                ans += 1\n",
    "            else:\n",
    "                ans += ((a & 1) + (b & 1))\n",
    "            a >>= 1\n",
    "            b >>= 1\n",
    "            c >>= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(32):\n",
    "            bit_a, bit_b, bit_c = (a >> i) & 1, (b >> i) & 1, (c >> i) & 1\n",
    "            if bit_c == 0:\n",
    "                ans += bit_a + bit_b\n",
    "            else:\n",
    "                ans += int(bit_a + bit_b == 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def count1(k):\n",
    "    count=0\n",
    "    while k:\n",
    "        k=k&(k-1)\n",
    "        count+=1\n",
    "    return count\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        k1 = c & (~a) & (~b)\n",
    "        k2 = (~c) & (a ^ b)\n",
    "        k3 = (~c) & a & b\n",
    "\n",
    "        return count1(k1)+count1(k2)+2*count1(k3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        op = 0\n",
    "        while c > 0:\n",
    "            a0,b0,c0 = a%2, b%2, c%2\n",
    "            if (a0 or b0) != c0:\n",
    "                if c0 == 1:\n",
    "                    op += 1\n",
    "                else:\n",
    "                    op += (a0+b0)\n",
    "            a = a // 2\n",
    "            b = b // 2\n",
    "            c = c // 2 \n",
    "        while a > 0:\n",
    "            op += a%2\n",
    "            a = a // 2\n",
    "        while b > 0:\n",
    "            op += b%2\n",
    "            b = b // 2\n",
    "        return op\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "\n",
    "        max_l = len(format(max(a, b, c), 'b'))\n",
    "        s = \"{0:0=%db}\" % max_l\n",
    "        aa = s.format(a)\n",
    "        bb = s.format(b)\n",
    "        cc = s.format(c)\n",
    "        r = 0\n",
    "        for i in range(max_l):\n",
    "            if cc[i] == '0':\n",
    "                r += 1 if aa[i] == '1' else 0\n",
    "                r += 1 if bb[i] == '1' else 0\n",
    "            else:\n",
    "                r += 1 if aa[i] == '0' and bb[i] == '0' else 0\n",
    "\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        ans = 0\n",
    "        while a > 0 or b > 0 or c > 0:\n",
    "            a_last = a & 1\n",
    "            b_last = b & 1\n",
    "            c_last = c & 1\n",
    "\n",
    "            if a_last | b_last == c_last:\n",
    "                pass\n",
    "            elif c_last == 1:\n",
    "                # a_last == b_last == 0\n",
    "                ans += 1\n",
    "            # c_last == 0\n",
    "            elif a_last & b_last == 1:\n",
    "                # a_last == b_last == 1\n",
    "                ans += 2\n",
    "            else:\n",
    "                # a_last == 1 or b_last == 1\n",
    "                ans += 1\n",
    "\n",
    "            a >>= 1\n",
    "            b >>= 1\n",
    "            c >>= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minFlips(self, a: int, b: int, c: int) -> int:\n",
    "        num = max(a,max(b,c))\n",
    "        a = bin(a).replace(\"0b\",\"\")\n",
    "        b = bin(b).replace(\"0b\",\"\")\n",
    "        c = bin(c).replace(\"0b\",\"\")\n",
    "        n = len(bin(num).replace(\"0b\",\"\"))\n",
    "        a=a.zfill(n)\n",
    "        b=b.zfill(n)\n",
    "        c=c.zfill(n)\n",
    "        count = 0\n",
    "        for i in range(n):\n",
    "            if a[i]==b[i]==c[i]:\n",
    "                continue\n",
    "            elif a[i]==b[i]==\"0\":\n",
    "                count+=1\n",
    "            elif a[i]==b[i]==\"1\":\n",
    "                count+=2\n",
    "            elif a[i]!=b[i] and c[i]==\"1\":\n",
    "                continue\n",
    "            elif a[i]!=b[i] and c[i]==\"0\":\n",
    "                count+=1\n",
    "        return count"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
